//
//  IdentityUserInfoVC.m
//  Peso Guide
//
//  Created by macbook on 2025/6/13.
//

#import "IdentityUserInfoVC.h"
#import "IdentityAlertView.h"
#import "CertificationCell.h"
#import "ProductDetailViewController.h"

@interface IdentityUserInfoVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) IdentityAlertView *alertView;

@property (nonatomic, strong) NSString *startTime;
@property (nonatomic, strong) NSString *endTime;

@property (nonatomic, strong) NSString *lng;
@property (nonatomic, strong) NSString *lat;

@end

@implementation IdentityUserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.startTime = [DataPointManager getCurrentTimestamp];
    
    [[LocationManager sharedManager] getCurrentLocationWithSuccess:^(CLLocationCoordinate2D coordinate) {
        DLog(@"get location success：log=%f, lat=%f", coordinate.longitude, coordinate.latitude);
        
        self.lng = [NSString stringWithFormat:@"%f",coordinate.longitude];
        self.lat = [NSString stringWithFormat:@"%f",coordinate.latitude];

        // 使用获取到的经纬度做其他操作...
        
    } failure:^(NSError *error) {
        // 处理错误情况...
    }];
    
    self.navView.backgroundColor = ColorHex(@"#1A7A35");
    self.titleLabel.text = [Lang set:@"Identity information"];
    self.backColor = ColorHex(@"#F3F4F8");
    
    CAGradientLayer *layer = [CAGradientLayer new];
    layer.colors = @[(__bridge id)ColorHex(@"#FFBD31").CGColor, (__bridge id)ColorHex(@"#FFFFFF").CGColor];
    layer.startPoint = CGPointMake(0.0, 0.0);
    layer.endPoint = CGPointMake(0.0, 1);
    layer.frame = CGRectMake(0, 0, KScreenWidth, KScreenWidth*0.736);
    [self.view.layer addSublayer:layer];
    
    UILabel *topLabel = [PesoKit createLabelWithFrame:CGRectMake(27, kStarBarHeight, KScreenWidth-54, 66) backGroundColor:[UIColor clearColor] text:[Lang set:@"Please check your lD information correctly.oncesubmilted it is not changed again"] textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 12)];
    topLabel.numberOfLines = 0;
    [self.view addSubview:topLabel];
    
    _whiteView = [PesoKit createViewWithFrame:CGRectMake(0, kStarBarHeight+KScreenWidth*0.736*0.24, KScreenWidth, KScreenHeight) backGroundColor:ColorHex(@"#FFFFFF")];
    _whiteView.clipsToBounds = YES;
    _whiteView.layer.cornerRadius = 15.0f;
    [self.view addSubview:_whiteView];
    
    [_whiteView addSubview:self.tableView];
    
    _nextButton = [PesoKit createButtonWithFrame:CGRectMake((KScreenWidth-214)/2.0, KScreenHeight-kSafeBottomHeight-30-44, 214, 44) backGroundColor:ColorHex(@"#FE7133") title:[Lang set:@"Next"] titleColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleMedium, 18)];
    _nextButton.clipsToBounds = YES;
    _nextButton.layer.cornerRadius = 22.0f;
    [_nextButton addTarget:self action:@selector(nextButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextButton];
    
    [self.view addSubview:self.navView];
    
//    _alertView = [[IdentityAlertView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
//    [self.view addSubview:_alertView];
    
    [[AWHttpService service] postWithUrl:@"/trains/inaccurate" params:@{@"across":self.productId} succ:^(id  _Nonnull responseObject) {
        if (SUCCESS) {
            NSDictionary *dataDic = [NSDictionary safeDictionaryWithDictionary:[responseObject safeObjectForKey:@"associating"]];
            self.dataArray = [NSMutableArray safeArrayWithArray:[dataDic safeObjectForKey:@"expanded"]];
            
            [self.tableView reloadData];
        }
    } fail:^(NSInteger errorType, id  _Nonnull error) {
        
    }];
}

- (void)nextButtonClick {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:self.productId forKey:@"across"];
    
    for (int i = 0; i < self.dataArray.count; i++) {
        NSDictionary *dataDic = [self.dataArray safeObjectAtIndex:i];
        NSString *interchangeably = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"interchangeably"]];
        NSString *fulfill = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"fulfill"]];
        
        [params setObject:[AppTools isEmpty:fulfill]?@"":fulfill forKey:[AppTools isEmpty:interchangeably]?@"":interchangeably];
    }
    
    DLog(@"params------%@",params);
    
    [[AWHttpService service] postWithUrl:@"/trains/formally" params:params succ:^(id  _Nonnull responseObject) {
        if (SUCCESS) {
            self.endTime = [DataPointManager getCurrentTimestamp];
            
            [[DataPointManager sharedManager] senRequestWithType:@"5" startTime:self.startTime endTime:self.endTime lng:self.lng lat:self.lat orderId:@""];

//            [[DataPointManager sharedManager] logEventWithType:@"5" startTime:self.startTime endTime:self.endTime orderId:@""];
            
            for (int i = 0; i < self.navigationController.viewControllers.count; i++) {
                UIViewController *vc = [self.navigationController.viewControllers safeObjectAtIndex:i];
                if ([vc isKindOfClass:[ProductDetailViewController class]]) {
                    [self.navigationController popToViewController:vc animated:YES];
                    return;
                }
                [self.navigationController popViewControllerAnimated:YES];
            }
        }else {
            NSString *order = [NSString stringWithFormat:@"%@",[responseObject safeObjectForKey:@"order"]];
            [SVProgressHUD showErrorWithStatus:order];
            [SVProgressHUD dismissWithDelay:3.0f];

        }
    } fail:^(NSInteger errorType, id  _Nonnull error) {
        
    }];
}

#pragma mark - UITableViewDelegate&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *CertificationCellIdentifier = [NSString stringWithFormat:@"CertificationCellIdentifier%ld",(long)indexPath.row];
    CertificationCell *cell = [tableView dequeueReusableCellWithIdentifier:CertificationCellIdentifier];
    if (!cell) {
        cell = [[CertificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CertificationCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.identityUserInfoVC = self;
    cell.index = indexPath.row;
    cell.dataDic = [self.dataArray safeObjectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 61;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - lazy load
- (BaseTableView *)tableView {
    if (!_tableView) {
        _tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-(kStarBarHeight+KScreenWidth*0.736*0.24)-(kSafeBottomHeight+30+44)-15) style:UITableViewStylePlain];
        _tableView.backgroundColor = ColorHex(@"#FFFFFF");
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
